﻿@using System.Text
@using OA.Model
@using OA.AspNetCore.Html
@using OA.Core.Model
@model OA.Model.Voucher
@{
    Layout = "~/Areas/Member/Views/Shared/_form_Layout.cshtml";
    CustomForm infoCustomForm = ViewBag.CustomForm;
    FormGroup infoFormGroup = ViewBag.FormGroup;
    string title = "新增" + infoCustomForm.Name;
    if (Model.ID > 0)
    {
        title = "编辑" + infoCustomForm.Name;
    }
    ViewBag.Title = title;
    string listTitle = infoCustomForm.Name + "列表";
    List<ExtendField> extendFields = ViewBag.ExtendFields;//控件样式
    Dictionary<string, string> dicExtend = ViewBag.DicExtend;//控件的值
    string msg = ViewBag.Msg;
    string tips = string.Empty;
    if (!string.IsNullOrWhiteSpace(msg))
    {
        tips = $"layer.msg('{msg}');";
    }
    List<ListItem> listPosition = ViewBag.ListPosition;
}
<link rel="stylesheet" type="text/css" href="@ViewBag.OssUrl/front/webuploader/webuploader.css">
<link rel="stylesheet" type="text/css" href="~/css/AddVoucher.css">
<script type="text/javascript" src="@ViewBag.OssUrl/front/webuploader/webuploader.js"></script>
<script type="text/javascript" src="@ViewBag.OssUrl/front/js/common.js"></script>
<script type="text/javascript" src="https://ciyun-oa.oss-cn-hangzhou.aliyuncs.com/front/js/wangEditor.min.js"></script>
<script type="text/javascript" src="~/js/base64.min.js"></script>
<script type="text/javascript" src="/js/xm-select.js"></script>
<script type="text/javascript" src="/Member/Home/GetXmSelectPositions"></script>
<script type="text/javascript" src="/Member/Home/GetXmSelectEmployees"></script>
<script type="text/javascript" src="/Member/Home/GetXmSelectDepartment"></script>
<script type="text/javascript" src="/js/AddVoucher.js"></script>

<div class="layui-my-content">
    <div class="myForm">
        @using (Html.BeginForm(FormMethod.Post, htmlAttributes: new { @class = "layui-form", id = "myForm" }))
        {
            @Html.AntiForgeryToken()
            if (!string.IsNullOrWhiteSpace(infoCustomForm.Explain))
            {
                <div style="padding:15px; text-align: left; background-color: #F2F2F2;margin:15px 0px 15px 15px;line-height:180%;">
                    <p>@Html.Raw(infoCustomForm.Explain.Replace("\r\n","<br/>"))</p>
                </div>
            }
            <div class="layui-form-item">
                @Html.LabelFor(model => model.Title, new { @class = "layui-form-label" })
                <div class="layui-input-block">
                    <input type="hidden" value="@Model.ID" name="ID" />
                    <input type="hidden" value="@Model.Status" name="Status" />
                    <input type="hidden" value="@Model.CustomFormID" name="CustomFormID" />
                    @Html.MyEditorFor(model => model.Title)
                </div>
            </div>
            @if (listPosition.Count == 1)
            {
                <input type="hidden" value="@listPosition[0].Value" name="PositionID" />

            }
            else
            {
                <div class="layui-form-item">
                    @Html.LabelFor(model => model.PositionID, new { @class = "layui-form-label" })
                    <div class="layui-input-block">
                        @Html.MyEditorFor(model => model.PositionID, listPosition)
                    </div>
                </div>
            }

            @if (infoCustomForm.IsCustomProcess || infoCustomForm.IsCustomProcess)
            {
                <div class="layui-form-item">
                    @if (infoCustomForm.IsCustomProcess)
                    {
                        <label class="layui-form-label">自定义流程</label>
                    }
                    else
                    {
                        <label class="layui-form-label">会签节点</label>
                    }

                    <div class="layui-input-block">
                        <input type="text" placeholder="请输入审批人员" id="IsCustomProcess" class="layui-input autocomplete" data-url="/Member/Home/GetEmployees" data-type="4" data-name="CustomProcessNode" value="">
                        <table class="layui-table IsCustomProcessTable" style="max-width:800px;">
                            <thead>
                                <tr><th>所属部门</th><th>岗位</th><th>审批人</th><th style="width:80px;">排序</th><th style="width:32px;">#</th></tr>
                            </thead>
                            <tbody>
                                @for (int i = 0; i < Model.CustomProcessNodes.Count; i++)
                                {
                                    CustomProcessNode item = Model.CustomProcessNodes[i];
                                    <tr data-id="@i" class="CustomProcessNode_@item.EmpID"><td><input type="hidden" class="DepartmentID" value="@item.DepartmentID" name="CustomProcessNode[@i].DepartmentID" /><input type="hidden" class="Status" value="@item.DepatrmentName" name="CustomProcessNode[@i].DepatrmentName" /> @item.DepatrmentName</td><td><input type="hidden" class="Status" value="@item.Status" name="CustomProcessNode[@i].Status" /><input type="hidden" value="@item.PositionID" name="CustomProcessNode[@i].PositionID" /><input type="hidden" value="@item.PositionName" name="CustomProcessNode[@i].PositionName" /> @item.PositionName</td><td>@item.EmpName <input type="hidden" value="@item.EmpName" name="CustomProcessNode[@i].EmpName" /><input type="hidden" value="@item.EmpID" name="CustomProcessNode[@i].EmpID" /></td><td><input type="text" class="layui-input" value="@item.Setp" name="CustomProcessNode[@i].Setp" style="max-width:80px;" /></td><td><span class="delete">删除</span></td></tr>
                                }
                            </tbody>
                        </table>
                    </div>
                </div>
            }

            if (extendFields != null && extendFields.Count > 0)
            {
                foreach (var item in extendFields)
                {
                    if (dicExtend.ContainsKey(item.Attribute))
                    {
                        item.Value = dicExtend[item.Attribute];
                    }
                    <div class="layui-form-item">
                        <label class="layui-form-label" for="@item.Attribute">@item.Name</label>
                        <div class="layui-input-block item_@item.Type">
                            @if (item.Type != 99)
                            {
                                @Html.Raw(MvcHtmlExtension.GetMyEditorControl(item, "dicExtend."))
                            }
                            else
                            {
                                List<Dictionary<string, string>> tableValues = new List<Dictionary<string, string>>();
                                if (!string.IsNullOrWhiteSpace(item.Value))
                                {
                                    tableValues = OA.Core.Helper.JsonHelper.DeserializeList<Dictionary<string, string>>(item.Value);
                                }
                                if (tableValues == null || tableValues.Count == 0)
                                {
                                    tableValues = new List<Dictionary<string, string>>() { new Dictionary<string, string>() };
                                }
                                ViewDataDictionary keyValuePairs = new ViewDataDictionary(ViewData);
                                keyValuePairs["TableInfo"] = item.Values;
                                keyValuePairs["TableAttribute"] = item.Attribute;
                                @Html.Partial("TableInfoForm", tableValues, keyValuePairs)
                            }
                        </div>
                    </div>
                }

            }

            <div class="layui-form-item">
                <div class="layui-input-block" style="text-align:right;">
                    <input type="button" value="提 交" lay-submit="" lay-filter="OAForm2" class="layui-btn mySubmit" />
                </div>
            </div>
        }
    </div>
</div>
<script type="text/javascript">
    @Html.Raw(tips)
    $(".webuploader-pick").html("选择文件");
    $(".mytablecontent").on('input', ".item_21 input", function (e) { automaticCalculation(e, this); });
    var relevancys = $(".relevancy");
    if (relevancys.length > 0) {
        for (var i = 0; i < relevancys.length; i++) {
            var attribute = $(relevancys[i]).attr("myattribute");
            if (attribute != undefined) {
                var myContact = null;
                if ($(relevancys[i]).parent().parent()[0].tagName == "TR") {
                    myContact = $(relevancys[i]).parent().parent().find(".Contact_" + attribute);
                }
                else {//div                    
                    myContact = $(relevancys[i]).parent().parent().parent().find(".item_44").find(".Contact_" + attribute);
                }
                console.log(myContact.length);
                if (myContact.length > 0) {
                    var tempValue = $(relevancys[i]).val();
                    if (tempValue != "") {
                        tempValue = Base64.decode(tempValue);
                        console.log(tempValue);
                        var info = $.parseJSON(tempValue);
                        $.ajaxSettings.async = false;
                        $.get("/Member/Home/GetContactByCompanyID?id=" + info.ID, function (result) {
                            var items = "";
                            $.each(result.List, function (index, item) {
                                items += "<option value='" + item.Info + "'>" + item.Contacts + "<option>"
                            });
                            $.each(myContact, function (index, item) {
                                console.log(items);
                                console.log(item);
                                $(item).html(items);
                                var currentvalue = $(this).attr("currentvalue");
                                $(item).val(currentvalue);
                            });                            
                        });
                        $.ajaxSettings.async = true;
                    }
                }
            }
        }
    }
    InintTable();
    InintCotrol();
</script>